clear all
*cap log close
set more off
set seed 603

// files to be read in --------
local NAMEIN1 = "outcomes_q2"
local NAMEIN2 = "adjust_q2"
local NAMEIN3 = "adjust_q2_drop"
local NAMEOUT = "table_adjust"
// -----------------------------------------------------


// Get numbers from the data -------------
use "${data}/out/4-main", clear
gen bunch = (harsh==0)
summ bunch if lenientpart==0
local a1 = "`:di %5.4f `r(mean)''"
di "`a1'"

bysort officerid: egen frac_bunch = mean(bunch)
keep if frac_bunch > 0 
summ bunch if lenientpart==0
local a2 = "`:di %5.4f `r(mean)''"
di "`a2'"
// ------------------------------------------


//// Initialize LaTeX Table ---------------------
capture erase "${out}/apx_extrap/`NAMEOUT'.tex"
file open fh using "${out}/apx_extrap/`NAMEOUT'.tex", write replace

file write fh ///	
	"&\multicolumn{4}{c}{\$Y_0\$} && \multicolumn{4}{c}{\$Y_1 - Y_0\$}\\" _n /// 
	"\cmidrule(l){2-5} \cmidrule(l){7-10}" _n ///
	"& (1) & (2) & (3) & (4) && (5) & (6) & (7) & (8)\\" _n ///
	"& All & \$D=1\$ & \$D=0\$ & Diff && All & \$D=1\$ & \$D=0\$ & Diff \\" _n ///
	"&     &         &         &      && (ATE) & (ATT) & (ATU) &  \\" _n ///
	"\hline" _n
	
	
// First part: driving history -------
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel A: Baseline specification}}}  \\"  _n ///
	
	
import delimited using "${est}/`NAMEIN1'.csv", clear
keep if yvar == "cite_ny1"
replace par = trim(upper(par))
gen g = 1
local glab1 = "    "

foreach q in 1 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

}	


// Second part: officer experience -------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{\underline{Panel B: Extrapolation to Z = `a1'}}}  \\"  _n ///
	

import delimited using "${est}/`NAMEIN2'.csv", clear
*keep if yvar == "cite_ny1"
replace par = trim(upper(par))
gen g = 1
local glab1 = "    "

foreach q in 1 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

}	


// Third part: officer experience (contesting-------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{8}{l}{\textit{Panel C: Extrapolation to Z = `a2'}}  \\"  _n ///	
	"\multicolumn{8}{l}{\underline{\textit{(Dropping officers with no citations at 9MPH)}}}  \\"  _n ///	
	
import delimited using "${est}/`NAMEIN3'.csv", clear
*keep if yvar == "cite_ny1"
replace par = trim(upper(par))
gen g = 1
local glab1 = "    "


foreach q in 1 {

local label = "`glab`q''"
local k = 1
foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
qui summ est if g == `q' & par == "`p'" 
local m`k' = "`:di %4.3f `r(mean)''"
qui summ se  if g == `q' & par == "`p'"
local s`k' = "(`:di %4.3f `r(mean)'')"
local ++k 
}

di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 


}	



file write fh "\bottomrule" _n  

file close fh
macro drop fh




